package cn.bj.yu.unsafe;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author 周宇
 * @create 2020-08-12 14:38
 * 同理可证：java.util.ConcurrentModificationException  并发修改异常
 */
public class UnSafeSet {
    public static void main(String[] args) {
        Set<String> set = new HashSet<>();
        /**
         * 解决方案：
         *  1、Set<String> set = Collections.synchronizedSet(new HashSet<>());
         *  2、Set<String> set = new CopyOnWriteArraySet<>();
         */
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}